package com.sharding.sphere.demo.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

/**
 * 对
 *
 * @author: shuguang.zhang
 * @date : 2021-06-04 15:16
 */
@Slf4j
public class TradeOrderDBPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> databaseNames, PreciseShardingValue<Long> preciseShardingValue) {
        log.info("TradeOrderDBPreciseShardingAlgorithm ---> databaseNames:[{}] complexKeysShardingValue:[{}]", databaseNames, preciseShardingValue);

        // 逻辑表名
        String logicTableName = preciseShardingValue.getLogicTableName();

        // 分片的字段值
        Long value = preciseShardingValue.getValue();

        // 分片的字段名
        String columnName = preciseShardingValue.getColumnName();

        BigInteger shardingValue = BigInteger.valueOf(value).mod(new BigInteger("2")).add(BigInteger.ONE);
        String key = logicTableName.concat("_")+shardingValue;

        return "m1";
    }
}
